package com.itheima.java.ai.langchin4j.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.java.ai.langchin4j.entity.SymptomAssessment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 症状自评 Mapper 接口
 *
 * @author xiaozhi
 * @since 2025-01-07
 */
@Mapper
public interface SymptomAssessmentMapper extends BaseMapper<SymptomAssessment> {

    /**
     * 根据用户ID分页查询症状评估记录
     *
     * @param page   分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    @Select("SELECT * FROM symptom_assessments WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC")
    IPage<SymptomAssessment> selectPageByUserId(Page<SymptomAssessment> page, @Param("userId") Long userId);

    /**
     * 根据风险等级查询评估记录
     *
     * @param riskLevel 风险等级
     * @return 评估记录列表
     */
    @Select("SELECT * FROM symptom_assessments WHERE risk_level = #{riskLevel} AND deleted = 0 ORDER BY created_at DESC")
    List<SymptomAssessment> selectByRiskLevel(@Param("riskLevel") String riskLevel);

    /**
     * 查询待审核的评估记录
     *
     * @param page 分页参数
     * @return 分页结果
     */
    @Select("SELECT * FROM symptom_assessments WHERE status = 'PENDING' AND deleted = 0 ORDER BY urgency_score DESC, created_at ASC")
    IPage<SymptomAssessment> selectPendingReviews(Page<SymptomAssessment> page);

    /**
     * 统计用户的症状评估数量
     *
     * @param userId    用户ID
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 统计数量
     */
    @Select("SELECT COUNT(*) FROM symptom_assessments WHERE user_id = #{userId} AND created_at BETWEEN #{startTime} AND #{endTime} AND deleted = 0")
    Integer countByUserIdAndDateRange(@Param("userId") Long userId, 
                                     @Param("startTime") LocalDateTime startTime, 
                                     @Param("endTime") LocalDateTime endTime);

    /**
     * 统计各风险等级的评估数量
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 统计结果
     */
    @Select("SELECT risk_level, COUNT(*) as count FROM symptom_assessments WHERE created_at BETWEEN #{startTime} AND #{endTime} AND deleted = 0 GROUP BY risk_level")
    List<Map<String, Object>> countByRiskLevelAndDateRange(@Param("startTime") LocalDateTime startTime, 
                                                          @Param("endTime") LocalDateTime endTime);

    /**
     * 查询用户最近的症状评估
     *
     * @param userId 用户ID
     * @param limit  限制数量
     * @return 评估记录列表
     */
    @Select("SELECT * FROM symptom_assessments WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC LIMIT #{limit}")
    List<SymptomAssessment> selectRecentByUserId(@Param("userId") Long userId, @Param("limit") Integer limit);

    /**
     * 根据推荐科室统计评估数量
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return 统计结果
     */
    @Select("SELECT recommended_department, COUNT(*) as count FROM symptom_assessments WHERE created_at BETWEEN #{startTime} AND #{endTime} AND deleted = 0 GROUP BY recommended_department")
    List<Map<String, Object>> countByDepartmentAndDateRange(@Param("startTime") LocalDateTime startTime, 
                                                           @Param("endTime") LocalDateTime endTime);
}